﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
using System.Data.OleDb;

namespace DAO
{
  public static  class MailInfoDAO
    {
      public static DataTable getMailInfoById(int id)
      {
          string strQuery = "select * from MailInformation where UserId=" + id;
          return  DataProvider.getTable(strQuery);
      }
      public static DataTable getMailInfoByMailAddress(string mail)
      {
          string strQuery = "select * from MailInformation where StatusUser= True and MailAddress='" + mail + "'";
          return DataProvider.getTable(strQuery);
      }
      public static bool updateMailInfo(MailInfoDTO mail)
      {
          string query = "UPDATE MailInformation SET MailAddress='" + mail.MailAddress  +
              "',MailServer='" + mail.MailServer + "',Port='" + mail.Port + "',TimeOut='" + mail.TimeOut +
              "',FolderIncome='" + mail.FolderIncome + "',FolderOutcome='" + mail.FolderOutcome +
              "',FolderBackup='" + mail.FolderBackup + "',RepettionTime='" + mail.RepettionTime + "',PassWordExcel='" + mail.PassWordExcel +
              "',Ext='"+mail.Ext+
              "',PassMail='" + mail.PassWord +"',SSl="+mail.SSl+" WHERE UserId =" + mail.UserId;
          return DataProvider.runQuery(query);
         
      }
      public static bool updateMailPass(string pass, int userId)
      {
          string query = "UPDATE MailInformation SET PassMail='" + pass + "' WHERE UserId =" + userId;
          return DataProvider.runQuery(query);
      }
      public static bool insertMailInfo(MailInfoDTO mail)
      {
          string query = "insert into MailInformation (UserId,MailAddress,PassMail,MailServer,Port,TimeOut,SSl,FolderIncome,FolderOutcome,FolderBackup,RepettionTime,PassWordExcel,MesseageMail,Ext,StatusUser) values (" +
              mail.UserId+",'"+mail.MailAddress+"','"+mail.PassWord+"','"+mail.MailServer+"','"+mail.Port+"','"+mail.TimeOut+"',"+mail.SSl+",'"+
              mail.FolderIncome+"','"+mail.FolderOutcome+"','"+mail.FolderBackup+"','"+mail.RepettionTime+"','"+mail.PassWordExcel+"','"+mail.MesseageMail+"','"+mail.Ext+"',True)";
          return DataProvider.runQuery(query);
      }
      public static DataTable getUserMailInfo(int userid)
      {
          string query = "select u.Username,m.MailAddress,m.FolderIncome,m.FolderOutcome,m.FolderBackup from MailInformation m inner join UserMail u on m.UserId=u.Id where u.StatusUser= True";
          return DataProvider.getTable(query);
      }
      public static bool deleteMailInfo(int id)
      {
          string query = "update MailInformation set StatusUser= false where UserId=" + id;
          return DataProvider.runQuery(query);
      }

    }
}
